package org.web.ctrlcv.board.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import org.web.ctrlcv.board.service.BoardService;

@Controller
@RequestMapping(value = "/web/board")
public class BoardController {
	private static final Logger logger = LoggerFactory.getLogger(BoardController.class);
	
	@Autowired
	BoardService boardService;

	@RequestMapping(value = "/{boardId}/list", method = RequestMethod.GET)
    public String selectBoardList(Locale locale, Model model, HttpServletRequest request, 
    		@RequestParam HashMap<String, String> params, @PathVariable("boardId") String boardId) throws Exception {
		logger.debug("<!>boardId : "+boardId);
		ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
		HashMap<String,String> map = new HashMap<String,String>();
		map.put("data1", "데이터1");
		map.put("data2", "데이터2");
		map.put("data3", "데이터3");
		map.put("data4", "데이터4");
		map.put("data5", "데이터5");
		list.add(map);
		map = new HashMap<String,String>();
		map.put("data1", "데이터1");
		map.put("data2", "데이터2");
		map.put("data3", "데이터3");
		map.put("data4", "데이터4");
		map.put("data5", "데이터5");
		list.add(map);
		map = new HashMap<String,String>();
		map.put("data1", "데이터1");
		map.put("data2", "데이터2");
		map.put("data3", "데이터3");
		map.put("data4", "데이터4");
		map.put("data5", "데이터5");
		list.add(map);
		map = new HashMap<String,String>();
		map.put("data1", "데이터1");
		map.put("data2", "데이터2");
		map.put("data3", "데이터3");
		map.put("data4", "데이터4");
		map.put("data5", "데이터5");
		list.add(map);
		map = new HashMap<String,String>();
		map.put("data1", "데이터1");
		map.put("data2", "데이터2");
		map.put("data3", "데이터3");
		map.put("data4", "데이터4");
		map.put("data5", "데이터5");
		list.add(map);
		model.addAttribute("list", list);
		return "board/selectBardList";
	}
	
	// /web/board/boardId/get/12
	@RequestMapping(value = "/{boardId}/get/{key}", method = RequestMethod.GET)
    public String selectBoard(Locale locale, Model model, HttpServletRequest request, 
    		@PathVariable("boardId") String boardId, @PathVariable("key") String key) throws Exception {
		return "main/dashboard";
	}
	
	@RequestMapping(value = "/{boardId}/insert", method = RequestMethod.GET)
    public String insertBoard(Locale locale,  @RequestParam HashMap<String, String> params, HttpServletRequest request, 
    		@PathVariable("boardId") String boardId) throws Exception {
		return "board/insertBoard";
	}
	
	@RequestMapping(value = "/{boardId}/insertHandle", method = RequestMethod.POST)
    public void insertBoardHandle(Locale locale, Model model, @RequestParam HashMap<String, String> paramMap, HttpServletRequest request, 
    		HttpServletResponse response, @PathVariable("boardId") String boardId) throws Exception {
		
		paramMap.put("RE_NO", "1");
			
		boardService.insertBoard(paramMap);

		JSONObject object = new JSONObject();
		object.put("RTN_CD", "0");
		object.put("RTN_MSG", "정상적으로 등록 되었습니다.");
		response.getWriter().write(object.toString());
	}
	
	@RequestMapping(value = "/{boardId}/update/{key}", method = RequestMethod.POST)
    public String updateBoard(Locale locale, @RequestParam HashMap<String, String> params, HttpServletRequest request,
    		@PathVariable("boardId") String boardId) throws Exception {
		return "main/dashboard";
	}
	
	@RequestMapping(value = "/{boardId}/delete/{key}", method = RequestMethod.POST)
    public String deleteBoard(Locale locale, Model model, HttpServletRequest request, 
    		@PathVariable("boardId") String boardId) throws Exception {
		return "main/dashboard";
	}
}
